### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")

# Panel A: Location

DiT_3_home <- feols(homicide_at_home_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_home, cluster="STATE")
DiT_3_outside <- feols(homicide_outside_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_outside, cluster="STATE")
DiT_3_other <- feols(homicide_other_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_other, cluster="STATE")

DiDiT_6_home <- feols(homicide_at_home_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_home, cluster="STATE")
DiDiT_6_outside <- feols(homicide_outside_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_outside, cluster="STATE")
DiDiT_6_other <- feols(homicide_other_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_other, cluster="STATE")

models <- list(DiT_3_home, DiT_3_outside, DiT_3_other, DiDiT_6_home, DiDiT_6_outside, DiDiT_6_other)
modelsummary(models, vcov = ~STATE,  output = "Figures and Tables/Table_5/Table_5_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

DiT_3_M <- feols(homicide_pc_morning~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_M, cluster="STATE")
DiT_3_A <- feols(homicide_pc_afternoon~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_A, cluster="STATE")
DiT_3_N <- feols(homicide_pc_night~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_N, cluster="STATE")

DiDiT_6_M <- feols(homicide_pc_morning~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_M, cluster="STATE")
DiDiT_6_A <- feols(homicide_pc_afternoon~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_A, cluster="STATE")
DiDiT_6_N <- feols(homicide_pc_night~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_N, cluster="STATE")

models <- list(DiT_3_M, DiT_3_A, DiT_3_N, DiDiT_6_M, DiDiT_6_A, DiDiT_6_N)
modelsummary(models, vcov = ~STATE, output =  "Figures and Tables/Table_5/Table_5_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
